package firear.remoteinput.example;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import firear.remoteinput.RemoteInput;
import firear.remoteinput.WebSocketCallback;

public class MainActivity extends Activity {

    private Button addButton(String text, View.OnClickListener l) {
        Button b = new Button(this);
        b.setText(text);
        b.setOnClickListener(l);
        return b;
    }

    private void startplay(String url) {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent("android.intent.action.VIEW", uri);
        intent.setComponent(new ComponentName("com.mxtech.videoplayer.pro", "com.mxtech.videoplayer.ActivityScreen"));
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout root = new LinearLayout(this);
        root.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        root.setOrientation(LinearLayout.VERTICAL);
        setContentView(root);

        RemoteInput.getInstance().setContext(this);
        root.addView(addButton("start", v -> {
            RemoteInput.getInstance().start(true, new WebSocketCallback() {
                @Override
                public void onStartFailed(String error) {

                }

                @Override
                public void onStartSucceed(String info) {

                }

                @Override
                public void onClientConnected() {

                }

                @Override
                public void onClientDisconnected() {

                }

                @Override
                public void onMessageReceived(String type, String data) {
                    Log.i("aa", type + ":" + data);
                    String trim = data.trim();
                    if (trim.length() != 0) {
                        if (trim.indexOf("://") < 0) {
                            trim = "http://" + trim;
                        }
                        startplay(trim);
                    }
                }
            });
        }));

        root.addView(addButton("play", v -> startplay("http://192.168.11.85/vv/mnt/%E5%9B%BD%E5%89%A7/rsj/22.1080p.HD%E5%9B%BD%E8%AF%AD%E4%B8%AD%E5%AD%97%E6%97%A0%E6%B0%B4%E5%8D%B0%5B66%E5%BD%B1%E8%A7%86www.66Ys.Co%5D.mp4")));
    }
}